package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.PersonOrDanweiView;
import com.gemme.gemme.view.RoundCheckBox;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvRightEditText;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class FaPiaoTaitouActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fapiao_taitou);
        initTop();
        initView();
        initIntentData();
    }
    ShopOrderInvoice bean;
    private void initIntentData() {
        try {
            String data = getIntent().getStringExtra("data");
            bean = JSON.parseObject(data, ShopOrderInvoice.class);
            initData();

        }catch (Exception e){

        }
    }



    PersonOrDanweiView personOrDanweiView;
    TvRightEditText fptt;
    TvRightEditText trt_shuihao;
    TvRightEditText trt_khh;
    TvRightEditText trt_yhzh;
    TvRightEditText trt_qydz;
    TvRightEditText trt_qydh;
    boolean isPerson = true;
    TextView tv_end;
    RoundCheckBox mrtt_checkbox;
    private void initView() {
        personOrDanweiView = findViewById(R.id.personOrDanweiView);
        fptt = findViewById(R.id.fptt);
        trt_shuihao = findViewById(R.id.trt_shuihao);
        trt_khh = findViewById(R.id.trt_khh);
        trt_yhzh = findViewById(R.id.trt_yhzh);
        trt_qydz = findViewById(R.id.trt_qydz);
        trt_qydh = findViewById(R.id.trt_qydh);
        tv_end = findViewById(R.id.tv_end);

        fptt.setLeftText("发票抬头").setRightHintText("填写抬头名称，默认为个人").setArrowVisible(View.VISIBLE);
        trt_shuihao.setLeftText("税号").setRightHintText("纳税人识别号或社会统一征信代码");
        trt_khh.setLeftText("开户银行").setRightHintText("选填");
        trt_yhzh.setLeftText("银行账号").setRightHintText("选填");
        trt_qydz.setLeftText("企业地址").setRightHintText("选填");
        trt_qydh.setLeftText("企业电话").setRightHintText("选填");
        isPerson = personOrDanweiView.getIsPerson();

        mrtt_checkbox = findViewById(R.id.mrtt_checkbox);
        changeState();
        setIsPersonListener();
        tv_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitData();
            }
        });
    }

    private void submitData() {
        String fpttstr = fptt.getContent();
        if(TextUtils.isEmpty(fpttstr)){
            Toast.makeText(mContext, "发票抬头不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(bean == null){
            bean = new ShopOrderInvoice();
        }

        if(isPerson){
            bean.setIsVat(0);
            bean.setInvoiceType(0);
            bean.setInvoiceTitle(fpttstr);
            bean.setDefaultType(mrtt_checkbox.isChecked() ? 0 : 1);
        }else{
            bean.setIsVat(0);
            bean.setInvoiceType(1);
            bean.setInvoiceTitle(fpttstr);
            bean.setInvoiceTaxNo(trt_shuihao.getContent() + "");
            bean.setVatCompanyName("");
            bean.setVatCompanyAddress(trt_qydz.getContent() + "");
            bean.setVatTelphone(trt_qydh.getContent() + "");
            bean.setVatBankName(trt_khh.getContent() + "");
            bean.setVatBankAccount(trt_yhzh.getContent() + "");
            bean.setDefaultType(mrtt_checkbox.isChecked() ? 0 : 1);
        }

        String jsonString = JSON.toJSONString(bean);
        Intent intent = new Intent();
        intent.putExtra("data",jsonString);
        setResult(1000,intent);

        finish();
    }
    Long id = -1L;
    private void initData() {
        try {
            if(bean == null)
                return;
            id = bean.getId();
            isPerson = bean.getInvoiceType() == 0 ? true : false;
            personOrDanweiView.setstate(isPerson);
                    fptt.setRightText(bean.getInvoiceTitle());
            trt_shuihao.setRightText(bean.getInvoiceTaxNo());
                    trt_khh.setRightText(bean.getVatBankName());
            trt_yhzh.setRightText(bean.getVatBankAccount());
                    trt_qydz.setRightText(bean.getVatCompanyAddress());
            trt_qydh.setRightText(bean.getVatTelphone());
            mrtt_checkbox.setChecked(bean.getDefaultType() == 0 ? true : false);
            changeState();
        }catch (Exception e){

        }
    }

    private void setIsPersonListener() {
        personOrDanweiView.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                isPerson = personOrDanweiView.getIsPerson();
                changeState();
            }
        });
    }



    private void changeState() {
        if(isPerson){
//            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_dowm);
//            fapiao_body_taitou.setVisibility(View.GONE);
          /*  trt_shuihao.setVisibility(View.GONE);
            iv_arrow.setVisibility(View.GONE);*/
            trt_shuihao.setVisibility(View.GONE);
            trt_khh.setVisibility(View.GONE);
            trt_yhzh.setVisibility(View.GONE);
            trt_qydz.setVisibility(View.GONE);
            trt_qydh.setVisibility(View.GONE);
        }else{
//            iv_arrow.setBackgroundResource(R.drawable.pc_arrow_up);
            /*trt_shuihao.setVisibility(View.VISIBLE);
            iv_arrow.setVisibility(View.VISIBLE);*/
            trt_shuihao.setVisibility(View.VISIBLE);
            trt_khh.setVisibility(View.VISIBLE);
            trt_yhzh.setVisibility(View.VISIBLE);
            trt_qydz.setVisibility(View.VISIBLE);
            trt_qydh.setVisibility(View.VISIBLE);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//            builder.setSmallIcon();
        }
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("发票抬头");
    }




}